﻿package mortal.game.view.copy.uniqueBoss.view
{
    import Message.Game.*;
    import Message.Public.*;
    import com.gengine.global.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import extend.language.*;
    import fl.data.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import frEngine.pools.*;
    import frEngine.primitives.object2d.*;
    import mortal.common.cd.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.rotate3dModelSprite.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.model.data.*;
    import mortal.game.scene3D.model.player.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.menu.*;
    import mortal.game.view.copy.uniqueBoss.*;
    import mortal.game.view.copy.uniqueBoss.renderer.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class UniqueBossMainWin extends BaseWindow
    {
        private var _bg:GImageBitmap;
        private var _rotate3d:Rotate3dModelSprite;
        private var _timeDes:GTextFiled;
        private var _cdTime:SecTimerView;
        private var _otherCdTime:SecTimerView;
        private var _accBtn:GLoadingButton;
        private var _startChallengeBtn:GLoadingButton;
        private var _stateImg:GImageBitmap;
        private var _leftTime:BitmapNumberText;
        private var _secTimeData:SecTimerViewData;
        private var _bossName:GImageBitmap;
        private var _bossDieBao:GTextFiled;
        private var _bossTexing:GTextFiled;
        private var _dropItem:BaseItem;
        private var _roleName:GTextFiled;
        private var _tips:GTextFiled;
        private var _myDamage:GTextFiled;
        private var _shoufuTxt:GTextFiled;
        private var _rankList:GTileList;
        private var _sUniquePanelInfo:SUniquePanelInfo;
        private var _totalDamagePlayerInfo:UniqueBossHurtPlayer;
        protected var _rect3d:Rect3DObject;
        protected var _img2d:Img2D;
        protected var _bodyPlayer:ActionPlayer;
        private static var _instance:UniqueBossMainWin;

        public function UniqueBossMainWin(param1:ILayer = null)
        {
            super(param1);
            setSize(889 - 2, 607 - 7);
            title = Language.getString(40954);
            this.layer = LayerManager.windowLayer3D;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap(ImagesConst.UniqueBossBg, 10, 39, null, this.onBgLoadCompl);
            this._rotate3d = UIFactory.getUICompoment(Rotate3dModelSprite, 10, 39, this.contentTopOf3DSprite);
            this._rotate3d.setSize(609, 561);
            this._cdTime = UIFactory.secTimeView(Language.getString(40955), 31 - 6, 561 - 4, 155, 25, this.contentTopOf3DSprite, GlobalStyle.textFormatLv.setSize(13));
            this._otherCdTime = UIFactory.secTimeView(Language.getString(40956), 31 - 6, 561 - 4 + 20, 208, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatLv);
            this._otherCdTime.visible = false;
            this._accBtn = UIFactory.gLoadingButton(ResFileConst.ClearCdBtn, 169 - 6, 559, 42, 21, this.contentTopOf3DSprite);
            this._accBtn.configEventListener(MouseEvent.CLICK, this.onResetCD);
            this._accBtn.filterEnabled = false;
            this._accBtn.mouseEnabled = false;
            this._startChallengeBtn = UIFactory.gLoadingButton(ResFileConst.tower_win_go2, 253 - 25, 548 + 3 - 8, 157, 39, this.contentTopOf3DSprite);
            this._startChallengeBtn.configEventListener(MouseEvent.CLICK, this.onChallengeHandler);
            this._stateImg = UIFactory.gImageBitmap("", 101, 113, this.contentTopOf3DSprite);
            this.pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.UniqueTxt, 246 - 20, 319 + 30, this.contentTopOf3DSprite));
            this._leftTime = UIFactory.bitmapNumberText(831 - 86 + 7, 52 - 6 + 464, "DoubleRewardTimerNum.png", 18.18, 27, 0, this.contentTopOf3DSprite, 10, BitmapNumberText.Mid);
            this._leftTime.customCharIndex = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, ::10};
            this._leftTime.text = "00:00:00";
            this._bossName = UIFactory.gImageBitmap(ImagesConst.UniqueBossName, 623 + 44, 109 - 6 + 15, this.contentTopOf3DSprite);
            this._bossDieBao = UIFactory.gTextField("", 638, 275 - 95, 230, 93, this.contentTopOf3DSprite, GlobalStyle.textFormatBai.setSize(12));
            this._bossDieBao.wordWrap = true;
            this._bossDieBao.multiline = true;
            this._bossDieBao.text = Language.getString(40957);
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.UniqueBossRightBottomText, 638 + 11, 275 + 290, this.contentTopOf3DSprite));
            this._roleName = UIFactory.gTextField("", 158 + 30 - 10 - 2, 141 - 8, 115, 30, this.contentTopOf3DSprite, GlobalStyle.textFormatLv.setSize(16).center());
            this._roleName.configEventListener(TextEvent.LINK, this.onLinkHandler);
            this._roleName.configEventListener(MouseEvent.MOUSE_OVER, this.onOverHandler);
            this._roleName.configEventListener(MouseEvent.MOUSE_OUT, this.onOutHandler);
            this._tips = UIFactory.gTextField(Language.getString(40958), 253 - 25, 548 + 30, 200, 20, this.contentTopOf3DSprite, GlobalStyle.textFormatHuang2);
            pushUIToDisposeVec(UIFactory.gImageBitmap(ImagesConst.SixBossRoleInfoBg, 11 + 619 - 7, 44 + 45 + 230 - 24, this.contentTopOf3DSprite));
            pushUIToDisposeVec(UIFactory.gTextField(Language.getString(40959), 11 + 619, 44 + 45 + 232 - 24, 267, 20, this.contentTopOf3DSprite));
            this._myDamage = UIFactory.gTextField("0", 420 + 200, 560 - 123, 300, 30, this.contentTopOf3DSprite, GlobalStyle.textFormatBai.center());
            this._rankList = UIFactory.tileList(11 + 619 - 7, 44 + 45 + 230, 267, 252, this.contentTopOf3DSprite);
            this._rankList.columnWidth = 262;
            this._rankList.rowHeight = 24;
            this._rankList.horizontalGap = 0;
            this._rankList.verticalGap = 0;
            this._rankList.setStyle("skin", new Bitmap());
            this._rankList.setStyle("cellRenderer", UniqueBossDamageRankCellRender);
            this._shoufuTxt = UIFactory.gTextField(Language.getString(40960), 831 - 86 + 7 - 149, 52 - 6 + 464, 300, 60, this.contentTopOf3DSprite, GlobalStyle.textFormatDanLan.setSize(20).setBold(true).center());
            this._shoufuTxt.visible = false;
            this.addListeners();
            GameProxy.copy.getUniqueBossHurtListInfo(this.sucFunc, this.failFunc);
            GameProxy.copy.getUniqueBossCopyPanelInfo(GameConst.UniqueBossCopyCode);
            return;
        }// end function

        private function sucFunc(param1:AMI_ICopy_getUniqueBossHurtList, param2:SUniqueBossHurtList) : void
        {
            var count:int;
            var o:UniqueBossHurtPlayer;
            var sortFunc:Function;
            var e:* = param1;
            var info:* = param2;
            this._myDamage.text = String(info.myHurt);
            var arr:* = new Array();
            var _loc_4:int = 0;
            var _loc_5:* = info.players;
            while (_loc_5 in _loc_4)
            {
                
                o = _loc_5[_loc_4];
                if (count < 5)
                {
                    arr.push(o);
                    count = (count + 1);
                }
            }
            sortFunc = function (param1:UniqueBossHurtPlayer, param2:UniqueBossHurtPlayer) : Number
            {
                if (param1 == null || param1 == null)
                {
                    return 1;
                }
                if (param1.hurt > param2.hurt)
                {
                    return -1;
                }
                if (param1.hurt < param2.hurt)
                {
                    return 1;
                }
                return 0;
            }// end function
            ;
            arr.sort(sortFunc);
            if (arr.length >= 1)
            {
                this._totalDamagePlayerInfo = arr[0];
            }
            this._rankList.dataProvider = new DataProvider(arr);
            this._rankList.drawNow();
            return;
        }// end function

        private function failFunc(param1:Object) : void
        {
            if (Global.isDebugModle)
            {
                MsgManager.showRollTipsMsg(Language.getString(40961));
            }
            return;
        }// end function

        private function addListeners() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.UniqueBossGetBossInfo, this.onGetBossInfo);
            NetDispatcher.addCmdListener(ServerCommand.UniqueBossClearCdSucc, this.onClearCdSucc);
            NetDispatcher.addCmdListener(ServerCommand.CopyEnterCopyFail, this.onEnterCopyFail);
            return;
        }// end function

        private function removeListeners() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.UniqueBossGetBossInfo, this.onGetBossInfo);
            NetDispatcher.removeCmdListener(ServerCommand.UniqueBossClearCdSucc, this.onClearCdSucc);
            NetDispatcher.removeCmdListener(ServerCommand.CopyEnterCopyFail, this.onEnterCopyFail);
            return;
        }// end function

        private function onBgLoadCompl() : void
        {
            if (isDisposed)
            {
                return;
            }
            this.init3DRect();
            this.add3dBg();
            this.initModel();
            return;
        }// end function

        protected function init3DRect() : void
        {
            if (this._rect3d == null)
            {
                this._rect3d = Rect3DManager.instance.registerWindow(true, new Rectangle(10, 39, 874, 562), this);
                Rect3DManager.instance.windowShowHander(null, this);
            }
            return;
        }// end function

        private function add3dBg() : void
        {
            if (this._rect3d)
            {
                this._rect3d.removeImg(this._img2d);
                this._img2d = new Img2D(null, this._bg.bitmapData, new Rectangle(0, 0, 874, 562), false);
                this._rect3d.addImg(this._img2d);
            }
            return;
        }// end function

        private function initModel() : void
        {
            var _loc_1:* = BossConfig.instance.getInfoByCode(GameConst.UniqueBossCode);
            if (_loc_1 == null)
            {
                return;
            }
            var _loc_2:* = _loc_1.mesh + ".md5mesh";
            var _loc_3:* = _loc_1.texture;
            var _loc_4:* = _loc_1.bone + ".skeleton";
            if (this._bodyPlayer)
            {
                this._rect3d.disposeObj3d(this._bodyPlayer);
            }
            this._bodyPlayer = FrObjectPool.getObject(ActionPlayer, this);
            this._bodyPlayer.changeAction(ActionName.Stand);
            this._bodyPlayer.selectEnabled = false;
            this._bodyPlayer.play();
            this._bodyPlayer.scaleValue = _loc_1.modelScale * 1 / 100;
            this._bodyPlayer.setRotation(0, 0, 0);
            this._bodyPlayer.load(_loc_2, _loc_4, _loc_3, this._rect3d.renderList);
            this._rect3d.addObject3d(this._bodyPlayer, 100 + 200, 100 + 270 + 60);
            this._rotate3d.model = this._bodyPlayer;
            return;
        }// end function

        private function updateAll() : void
        {
            this.updateState();
            this.updateCDTime();
            this.updateOtherCDTime();
            this.updateLeftTime();
            this.updateBossName();
            this.updateDropItem();
            this.updateChallengeState();
            return;
        }// end function

        private function updateState() : void
        {
            var _loc_1:Date = null;
            var _loc_2:Date = null;
            var _loc_3:int = 0;
            if (this._sUniquePanelInfo.isDead)
            {
                this._stateImg.imgUrl = ImagesConst.UniqueBoss_State_1;
                this._roleName.htmlText = HTMLUtil.addLink(this._sUniquePanelInfo.successPlayer.name, GlobalStyle.colorLv);
                this._leftTime.visible = false;
                this._shoufuTxt.visible = true;
            }
            else
            {
                this._roleName.text = "";
                _loc_1 = ClockManager.instance.serverOpenDate;
                _loc_2 = new Date(_loc_1.time);
                _loc_2.date = _loc_2.date + 3;
                _loc_3 = UniqueBossUtil.getLeftTime();
                if (_loc_3 > 0)
                {
                    this._stateImg.imgUrl = ImagesConst.UniqueBoss_State_2;
                }
                else if (this._totalDamagePlayerInfo)
                {
                    this._leftTime.visible = false;
                    this._shoufuTxt.visible = true;
                    this._stateImg.imgUrl = ImagesConst.UniqueBoss_State_1;
                    this._roleName.htmlText = HTMLUtil.addLink(this._totalDamagePlayerInfo.playerName, GlobalStyle.colorLv);
                }
                else
                {
                    this._stateImg.imgUrl = ImagesConst.UniqueBoss_State_3;
                }
            }
            return;
        }// end function

        private function updateCDTime() : void
        {
            if (this._sUniquePanelInfo.nextCdSeconds > 0)
            {
                this._accBtn.filterEnabled = true;
                this._accBtn.mouseEnabled = true;
            }
            this._cdTime.setLeftTime(this._sUniquePanelInfo.nextCdSeconds);
            this._cdTime.timeOutHandler = this.timeOutHandler;
            return;
        }// end function

        private function timeOutHandler() : void
        {
            this._accBtn.filterEnabled = false;
            this._accBtn.mouseEnabled = false;
            return;
        }// end function

        private function updateOtherCDTime() : void
        {
            if (this._sUniquePanelInfo.nextSecondForYou > 0)
            {
                this._otherCdTime.visible = true;
            }
            else
            {
                this._otherCdTime.visible = false;
            }
            this._otherCdTime.setLeftTime(this._sUniquePanelInfo.nextSecondForYou);
            this._otherCdTime.timeOutHandler = this.timeOutHandler2;
            return;
        }// end function

        private function timeOutHandler2() : void
        {
            this._otherCdTime.visible = false;
            return;
        }// end function

        private function updateLeftTime() : void
        {
            var _loc_1:* = UniqueBossUtil.getLeftTime();
            if (_loc_1 > 0 && this._secTimeData == null)
            {
                this._secTimeData = new SecTimerViewData();
                this._secTimeData.setLeftTime(int(_loc_1 / 1000));
                this._secTimeData.caller = this.onTimeChange;
                this._secTimeData.timeOutCaller = this.onTimeOut;
            }
            return;
        }// end function

        private function updateBossName() : void
        {
            return;
        }// end function

        private function updateDropItem() : void
        {
            return;
        }// end function

        private function updateChallengeState() : void
        {
            var _loc_1:* = Cache.instance.role.entityInfo.combat;
            if (_loc_1 >= 60000)
            {
                this._tips.visible = false;
                this._startChallengeBtn.filterEnabled = true;
                this._startChallengeBtn.mouseEnabled = true;
            }
            else
            {
                this._tips.visible = true;
                this._startChallengeBtn.filterEnabled = false;
                this._startChallengeBtn.mouseEnabled = false;
            }
            return;
        }// end function

        private function onResetCD(event:MouseEvent) : void
        {
            var _loc_2:* = Math.ceil(this._cdTime.secTimerViewData.getMinutes() / 5);
            GameProxy.copy.clearEnterCopyDiCd(GameConst.UniqueBossCopyCode, _loc_2);
            return;
        }// end function

        private function onClearCdSucc(param1:Object) : void
        {
            this._cdTime.setLeftTime(0);
            this.timeOutHandler();
            return;
        }// end function

        private function onChallengeHandler(event:MouseEvent) : void
        {
            var _loc_3:String = null;
            var _loc_2:* = Cache.instance.role.entityInfo.combat;
            if (_loc_2 < 30000)
            {
                _loc_3 = Language.getString(40962);
                MsgManager.showRollTipsMsg(_loc_3);
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.UniqueBossEnterCopy));
            return;
        }// end function

        private function onTimeChange() : void
        {
            this._leftTime.text = this._secTimeData.HH() + ":" + this._secTimeData.mm() + ":" + this._secTimeData.ss();
            return;
        }// end function

        private function onTimeOut() : void
        {
            return;
        }// end function

        private function onGetBossInfo(param1:Object) : void
        {
            var _loc_2:* = param1 as SUniquePanelInfo;
            this._sUniquePanelInfo = _loc_2;
            if (_loc_2)
            {
                this.updateAll();
            }
            return;
        }// end function

        private function remove3D() : void
        {
            if (this._rect3d)
            {
                if (this._img2d)
                {
                    this._rect3d.removeImg(this._img2d);
                    this._img2d = null;
                }
                if (this._bodyPlayer)
                {
                    this._rect3d.disposeObj3d(this._bodyPlayer);
                    this._bodyPlayer = null;
                }
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            return;
        }// end function

        private function onLinkHandler(event:TextEvent) : void
        {
            var _loc_2:SEntityId = null;
            PlayerMenuRegister.UnRegister(this._roleName);
            if (this._sUniquePanelInfo.isDead)
            {
                PlayerMenuRegister.Register(this._roleName, this._sUniquePanelInfo.successPlayer, PlayerMenuConst.FriendOpMenu);
            }
            else if (this._totalDamagePlayerInfo)
            {
                _loc_2 = Cache.instance.role.entityInfo.entityId;
                _loc_2.id = this._totalDamagePlayerInfo.playerId;
                PlayerMenuRegister.Register(this._roleName, Cache.instance.entity.getEntityInfoById(_loc_2), PlayerMenuConst.FriendOpMenu);
            }
            return;
        }// end function

        private function onOverHandler(event:MouseEvent) : void
        {
            CursorManager.showCursor(CursorManager.BUTTON);
            return;
        }// end function

        private function onOutHandler(event:MouseEvent) : void
        {
            CursorManager.hideFromButton();
            return;
        }// end function

        private function onEnterCopyFail(param1:Object) : void
        {
            var _loc_2:* = param1 as int;
            if (_loc_2 == GameConst.UniqueBossCopyCode)
            {
                GameProxy.copy.getUniqueBossCopyPanelInfo(GameConst.UniqueBossCopyCode);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._bg.dispose(param1);
            this._bg = null;
            this._rotate3d.dispose(param1);
            this._rotate3d = null;
            this._accBtn.dispose(param1);
            this._accBtn = null;
            this._startChallengeBtn.dispose(param1);
            this._startChallengeBtn = null;
            this._stateImg.dispose(param1);
            this._stateImg = null;
            if (this._secTimeData)
            {
                this._secTimeData.dispose();
                this._secTimeData = null;
            }
            this._bossName.dispose(param1);
            this._bossName = null;
            this._bossDieBao.dispose(param1);
            this._bossDieBao = null;
            this._roleName.dispose(param1);
            this._roleName = null;
            this._cdTime.dispose(param1);
            this._cdTime = null;
            this._otherCdTime.dispose(param1);
            this._otherCdTime = null;
            this._tips.dispose(param1);
            this._tips = null;
            this._rankList.dispose(param1);
            this._rankList = null;
            this._myDamage.dispose(param1);
            this._myDamage = null;
            this._leftTime.dispose(param1);
            this._leftTime = null;
            this._shoufuTxt.dispose(param1);
            this._shoufuTxt = null;
            this._sUniquePanelInfo = null;
            this.remove3D();
            this.removeListeners();
            return;
        }// end function

        public static function get instance() : UniqueBossMainWin
        {
            if (_instance == null)
            {
                _instance = new UniqueBossMainWin;
            }
            return _instance;
        }// end function

        public static function get isViewShow() : Boolean
        {
            return _instance && !_instance.isHide;
        }// end function

    }
}
